import Vue from 'vue';
import VueRouter, { RouteConfig } from 'vue-router';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    redirect: '/home/work',
    children: [
      {
        path: '/home/flight',
        component: () => import('../views/demo/FlightView.vue'),
        meta: {
          title: '飞机票信息'
        }
      },
      {
        path: '/home/work',
        component: () => import('../views/demo/WorkView.vue'),
        meta: {
          title: '工作台'
        }
      },
      {
        path: '/home/employee',
        component: () => import('../views/Employee/EmployeeView.vue'),
        meta: {
          title: '员工信息'
        }
      },
      {
        path: '/home/order',
        component: () => import('../views/demo/OrderView.vue'),
        meta: {
          title: '订单信息'
        }
      },
      {
        path: '/home/employee/add',
        component: () => import('../views/Employee/addEmployee.vue'),
        meta: {
          title: '添加员工'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/loginView.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/404',
    component: () => import('../views/404View.vue'),
    meta: {
      title: '404，您所查找的页面不翼而飞~'
    }
  },
  {
    path: '*',
    redirect: '/404'
  }
];

const router = new VueRouter({
  routes
});

router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  } else {
    document.title = '默认标题';
  }

  // 开始进度条
  NProgress.start();
  next();
});

// 路由跳转完成后停止进度条
router.afterEach(() => {
  NProgress.done();
});

export default router;
